package com.aq.sales.business.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aq.sales.business.intf.TestBO;
import com.aq.sales.entity.Lookup;
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class TestBOImpl extends CommonBOImpl<Lookup> implements TestBO<Lookup> {

		@Override
	public List<Lookup> findAll(int accId) {
		Query qry=getCurrentSession().createQuery("from Lookup where accId=:accId");
		qry.setInteger("accId", accId);
		return qry.list();
	}
	

	@Override
	public int insert(Lookup obj) {
		getCurrentSession().save(obj);
		getCurrentSession().flush();
		return 0;
	}

	@Override
	@Transactional
	public int update(Lookup obj) {
		getCurrentSession().saveOrUpdate(obj);
		return 0;
	}

	@Override
	public int delete(Lookup obj) {
		getCurrentSession().delete(obj);
		return 0;
	}


	@Override
	public Lookup findById(int id) {
		throw new RuntimeException("This method is not Supported");
	}

	@Override
	public Lookup findById(int id, int accId) {
		Query qry=getCurrentSession().createQuery("from Lookup where id=:id AND accId=:accId");
		qry.setInteger("id", id);
		qry.setInteger("accId", accId);
		return (Lookup)qry.uniqueResult();
	}
	@Transactional(propagation=Propagation.REQUIRED)
	public int insert(Lookup p,boolean t) {
		String sql = "INSERT INTO LOOKUP(NAME, DESCRIPTION, PARENTID, ACC_ID, VALUE) 	VALUES 	(?,?,?,?,?)";
		p.setDescription(p.getDescription()+"JDBC");
		Object[] parms=new Object[]{p.getName(),p.getDescription(),p.getParentid(),p.getAccId(),p.getValue()};
		getJdbcTemplate().update(sql,parms);
		p.setDescription(p.getDescription()+"HIB");
		insert(p);
		if(true)throw new RuntimeException();
		return 0;
	}


	@Override
	public List<Lookup> query(String clause, int accId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
